<template>
  <div>
    <a-table
      :locale="{emptyText:'暂无数据'}"
      :columns="columns" :data-source="dataList" size="small">
      <template #name="{ record }">
        <a @click="toUpdatePage(record.id)">{{ record.name }}</a>
      </template>
      <template #tags="{ text: tags }">
      <span>
        <a-tag
          v-for="tag in tags"
          :key="tag"
          :color="tag === '工作负责人' ? 'geekblue' : 'green'"
        >
          {{ tag.toUpperCase() }}
        </a-tag>
      </span>
      </template>
      <template #action="{ record }">
      <span>
        <a @click="toUpdatePage(record.id)">修改</a>
        <a-divider type="vertical"/>
        <a-popconfirm
          title="确定要删除吗?"
          ok-text="确定"
          cancel-text="取消"
          @confirm="toDelete(record.id)"
        >
          <a>删除</a>
        </a-popconfirm>
      </span>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts">
import { defineComponent, getCurrentInstance, ref } from 'vue'
import { ResultModel } from '@/common/result.model'
import { TeamModel } from '@/views/team/js/team-model'
import { message } from 'ant-design-vue'
import { PersonApi } from '@/views/person/js/person-api'

const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    slots: {
      title: 'customTitle',
      customRender: 'name'
    }
  },
  {
    title: '用户名',
    dataIndex: 'username',
    key: 'age'
  },
  {
    title: '所属班组',
    dataIndex: 'teamName',
    key: 'teamName'
  },
  {
    title: '标签',
    key: 'tags',
    dataIndex: 'tags',
    slots: { customRender: 'tags' }
  },
  {
    title: '操作',
    key: 'action',
    slots: { customRender: 'action' }
  }
]

export default defineComponent({
  data () {
    return {
      name: null
    }
  },
  setup () {
    const instance = getCurrentInstance()
    const dataList = ref([])
    const pageQuery = (name = '') => {
      PersonApi.query(1, 10, name).then(response => {
        console.log(response)
        if (ResultModel.isSuccessPageInfo(response)) {
          // eslint-disable-next-line no-return-assign
          response.data && response.data.dataList && response.data.dataList.forEach((item: TeamModel) => item.key = item.id)
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          dataList.value = response.data.dataList
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          console.log('人员信息结果：', dataList.value)
        }
      })
    }
    pageQuery()
    const toDelete = (id: string) => {
      PersonApi.delete(id).then(response => {
        if (ResultModel.isSuccess(response)) {
          message.success({ content: '删除成功！' })
          pageQuery()
        } else {
          message.error({ content: '删除失败！' })
        }
      })
    }
    const toUpdatePage = (id: string) => {
      if (instance && instance.proxy && instance.proxy.$root && instance.proxy.$root.$router) {
        instance.proxy.$root.$router.push('/person/person/form/' + id)
      }
    }
    const toDetailPage = (id: string) => {
      if (instance && instance.proxy && instance.proxy.$root && instance.proxy.$root.$router) {
        instance.proxy.$root.$router.push('/person/person/form/' + id)
      }
    }
    return {
      pageQuery,
      toDelete,
      toUpdatePage,
      toDetailPage,
      dataList,
      columns
    }
  }
})
</script>
